package com.ys.chatserver.tool;

import cn.hutool.core.lang.UUID;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class IdUtils {

    /**
     * 大小写字母+数字
     */
    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    /**
     * 获取8位的UUID
     *
     * @return 8位的UUID
     * @author afěi
     */
    public static String get8Uuid() {
        StringBuilder builder = new StringBuilder();
        final String uuid = simpleUuid();
        final int length = 8;
        for (int i = 0; i < length; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            builder.append(chars[x % 0x3E]);
        }
        return builder.toString();
    }

    /**
     * 简化的UUID，去掉了横线
     *
     * @return 简化的UUID，去掉了横线
     * @author afěi
     */
    public static String simpleUuid() {
        return UUID.randomUUID().toString(true);
    }

    public static String getNumberUuid() {
        Random rnd = new Random();
        LocalTime now = LocalTime.now();
        LocalDate today = LocalDate.now();
        return String.format("%02d%03d%05d", today.getYear() - 2020, today.getDayOfYear(), now.toSecondOfDay()) +
                rnd.nextInt(100);
    }

}
